import React, {Component} from "react";
import {InputItem,NavBar,Icon,WhiteSpace,WingBlank,List,TextareaItem,Button} from 'antd-mobile'
import { createForm } from 'rc-form';
import "./subPages.css"
import axios from "axios"

class PublishArticle extends React.Component{

    constructor(props) {
        super(props);
    }
    Article(){
        var article_title=this.props.form.getFieldValue('article_title');
        var article=this.props.form.getFieldValue('article');
        if(article_title===undefined||article===undefined){
            alert("文章标题或内容不能为空！");
            return;
        }else if(article_title===""||article===""){
            alert("文章标题或内容不能为空！");
            return;
        }
        var getPhoneNum=document.cookie.split('=')[1]
        axios.get(global.routes.database+'/setArticle',{
            params:{
                phoneNum:getPhoneNum,
                title:article_title,
                text:article
            }
        }).then((res)=>{
            if(res.data.Code==1){
                alert("文章上传成功");
                window.location.href = '#/terran/publish';
            }
        })
    }

    render() {
        const { getFieldProps } = this.props.form;
        return (
            <div>
                <NavBar
                    className="publish-subPages-NavBar"
                    mode="light"
                    icon={<Icon type="left" />}
                    onLeftClick={() => {
                        window.location.href = '#/terran/publish'
                    }}
                    rightContent={[
                        <Icon key="0" type="search" style={{ marginRight: '16px' }} />,
                        <Icon key="1" type="ellipsis" />,
                    ]}
                >发布文章</NavBar>
                <WhiteSpace />
                <List>
                    <TextareaItem
                        {...getFieldProps('article_title')}
                        clear
                        placeholder="在此处输入文章的标题"
                        ref={el => this.autoFocusInst = el}
                    >标题</TextareaItem>
                </List>
                <WhiteSpace />
                <List>
                    <TextareaItem
                        {...getFieldProps('article')}
                        rows={10}
                        placeholder="文章正文"
                    />
                </List>
                <WhiteSpace />
                <WhiteSpace />
                <WhiteSpace />
                <WhiteSpace />
                <WhiteSpace />
                <WingBlank style={{position:"relative"}}>
                    <Button type="primary" onClick={this.Article.bind(this)}>发布</Button>
                </WingBlank>
            </div>
        );
    }
}
const PublishArticleWrapper = createForm()(PublishArticle);
export default PublishArticleWrapper;